#include "world.h"

World::World(QObject *parent) :
    QObject(parent)
{

    initMap(5);

    if(!openFileWithCommands())
        qDebug()<<"Can't open file with commands";
    if(!openFileWithItems())
        qDebug()<<"Can't open file with items";
    if(!openFileWithCreatures())
        qDebug()<<"Can't open file with creatures";
    if(!openFileWithRegions())
        qDebug()<<"Can't open file with regions";

    QString name = QString("Ololo");
    this->_player = new Player(name,this);

}

void World::initMap(int size)
{
    for(int x = 0 ; x< size ; x++)
    {
        QVector<GameRegion *> row;
        _map.append(row);
        for(int y = 0 ; y<size ; y ++)
        {
            GameRegion * reg = NULL;
            _map[x].append(reg);
        }
    }
}

void World::getCommandFromWindow(QString str)
{
    this->_player->doSomeCommand(str);
}

bool World::openFileWithCommands(void)
{
    QFile *file = new QFile(":/main/commands");
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
             return false;
    QTextStream in(file);
    QString str = in.readLine();
    while (!str.isNull())
    {
        QStringList list = str.split(" ");
        int data = list.at(0).toInt();
        QString key = list.at(1);
        _global_commands.insert(key,data);
        str = in.readLine();
    }
    return true;
}

bool World::openFileWithItems(void)
{
    QFile *file = new QFile(":/main/items");
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
             return false;
    QTextStream in(file);
    QString str = in.readLine();
    while (!str.isNull())
    {
        QStringList list = str.split(" ");
        int data = list.at(0).toInt();
        for(int i = 1 ; i< list.size(); i++){
            QString key = list.at(i);
            _global_items.insert(key,data);
        }
        str = in.readLine();
    }
    return true;
}

bool World::openFileWithCreatures(void)
{
    QFile *file = new QFile(":/main/creatures");
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
             return false;
    QTextStream in(file);
    QString str = in.readLine();
    while (!str.isNull())
    {
        QStringList list = str.split(" ");
        int data = list.at(0).toInt();
        for(int i = 1 ; i< list.size(); i++){
            QString key = list.at(i);
            _global_creatures.insert(key,data);
        }

        str = in.readLine();
    }
    return true;
}

bool World::loadRegion(QStringList &strings)
{
    QString description;
    QStringList monsters;
    QStringList quests;
    QString passToImage;
    int i=1;
    int x = strings.at(1).toInt();
    int y = strings.at(2).toInt();
    QPoint pos(x,y);
    QString name = strings.at(0);
    do
    {
        description.append(strings.at(i) + " ");
        i++;
    }while(!strings.at(i).contains("Monsters"));
    i++;
    do
    {
        monsters.append(strings.at(i));
        i++;
    }while(!strings.at(i).contains("Quest"));
    i++;
    do
    {
        quests.append(strings.at(i));
        i++;
    }while(!strings.at(i).contains("Image"));
    i++;

    passToImage = strings.at(i);

    GameRegion *reg = new GameRegion(pos,name,description,passToImage);
    addRegion(reg);
}

bool World::addRegion(GameRegion *reg)
{
    int x = reg->getPos().x();
    int y = reg->getPos().y();
    _map[x][y] = reg;
}

bool World::openFileWithRegions(void)
{
    QStringList list;
    QFile *file = new QFile(":/main/regions");
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
             return false;
    QTextStream in(file);
    QString str = in.readLine();
    while (!str.isNull())
    {
        list.append(str);
        if(str.contains("end"))
        {
            loadRegion(list);
            list.clear();
        }
        str = in.readLine();
    }
    return true;
}

int World::getItemId(QString &str)
{
    return _global_items.value(str);
}
int World::getCommandId(QString &str)
{
    return _global_commands.value(str);
}
int World::getCreatureId(QString &str)
{
    return _global_creatures.value(str);
}
